/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZDataSource;
/*     */ import com.zimbra.client.ZFilterAction;
/*     */ import com.zimbra.client.ZFilterAction.ZFileIntoAction;
/*     */ import com.zimbra.client.ZFilterCondition;
/*     */ import com.zimbra.client.ZFilterCondition.HeaderOp;
/*     */ import com.zimbra.client.ZFilterCondition.ZHeaderCondition;
/*     */ import com.zimbra.client.ZFilterRule;
/*     */ import com.zimbra.client.ZFilterRules;
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZPop3DataSource;
/*     */ import com.zimbra.common.account.Key.DataSourceBy;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.db.DbPop3Message;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.soap.admin.type.DataSourceType;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.mail.internet.MailDateFormat;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestPop3Import
/*     */   extends TestCase
/*     */ {
/*     */   private static final String USER_NAME = "user1";
/*     */   private static final String USER2_NAME = "user2";
/*  54 */   private static final String NAME_PREFIX = TestPop3Import.class.getSimpleName();
/*  55 */   private static final String DATA_SOURCE_NAME = NAME_PREFIX;
/*  56 */   private static final String TEMP_USER_NAME = NAME_PREFIX + "Temp";
/*     */   private ZFilterRules mOriginalRules;
/*     */   private boolean mIsServerSideTest;
/*     */   
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  63 */     this.mIsServerSideTest = false;
/*  64 */     cleanUp();
/*  65 */     createDataSource();
/*  66 */     this.mOriginalRules = TestUtil.getZMailbox("user1").getIncomingFilterRules();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testBogusDate()
/*     */     throws Exception
/*     */   {
/*  74 */     Provisioning.getInstance().createAccount(TestUtil.getAddress(TEMP_USER_NAME), "test123", null);
/*     */     
/*     */ 
/*  77 */     MailDateFormat format = new MailDateFormat();
/*  78 */     Date date = format.parse("Thu, 31  Aug 2039 10:29:46 +0800");
/*  79 */     String message = TestUtil.getTestMessage(NAME_PREFIX + " testBogusDate", null, null, date);
/*  80 */     ZMailbox remoteMbox = TestUtil.getZMailbox(TEMP_USER_NAME);
/*  81 */     String folderId = Integer.toString(2);
/*  82 */     remoteMbox.addMessage(folderId, null, null, 0L, message, true);
/*     */     
/*     */ 
/*  85 */     ZMailbox localMbox = TestUtil.getZMailbox("user1");
/*  86 */     ZPop3DataSource ds = getZDataSource();
/*  87 */     ds.setUsername(TEMP_USER_NAME);
/*  88 */     ds.setEnabled(true);
/*  89 */     localMbox.modifyDataSource(ds);
/*     */     
/*     */ 
/*  92 */     List<ZMessage> messages = TestUtil.search(localMbox, "in:inbox " + NAME_PREFIX);
/*  93 */     assertEquals("Found unexpected message in local inbox", 0, messages.size());
/*  94 */     TestUtil.importDataSource(ds, localMbox, remoteMbox);
/*  95 */     messages = TestUtil.search(localMbox, "in:inbox " + NAME_PREFIX);
/*  96 */     assertEquals("Imported message not found", 1, messages.size());
/*     */   }
/*     */   
/*     */ 
/*     */   public void testTestDataSource()
/*     */     throws Exception
/*     */   {
/* 103 */     ZMailbox localMbox = TestUtil.getZMailbox("user1");
/* 104 */     ZPop3DataSource ds = getZDataSource();
/* 105 */     ds.setUsername("user2");
/* 106 */     localMbox.modifyDataSource(ds);
/* 107 */     assertNull(localMbox.testDataSource(ds));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testFiltering()
/*     */     throws Exception
/*     */   {
/* 116 */     String folderPath = "/" + NAME_PREFIX + "-testFiltering";
/* 117 */     String filteredPath = "/" + NAME_PREFIX + "-testFiltering-filtered";
/*     */     
/*     */ 
/* 120 */     Provisioning.getInstance().createAccount(TestUtil.getAddress(TEMP_USER_NAME), "test123", null);
/*     */     
/*     */ 
/* 123 */     ZMailbox remoteMbox = TestUtil.getZMailbox(TEMP_USER_NAME);
/* 124 */     TestUtil.addMessage(remoteMbox, NAME_PREFIX + " testFiltering");
/*     */     
/*     */ 
/* 127 */     ZMailbox localMbox = TestUtil.getZMailbox("user1");
/* 128 */     localMbox.getFolderByPath("/Inbox");
/* 129 */     ZFolder dsFolder = TestUtil.createFolder(localMbox, folderPath);
/* 130 */     TestUtil.createFolder(localMbox, filteredPath);
/*     */     
/*     */ 
/* 133 */     List<ZFilterRule> rules = new ArrayList();
/* 134 */     List<ZFilterCondition> conditions = new ArrayList();
/* 135 */     List<ZFilterAction> actions = new ArrayList();
/* 136 */     conditions.add(new ZFilterCondition.ZHeaderCondition("subject", ZFilterCondition.HeaderOp.CONTAINS, "testFiltering"));
/* 137 */     actions.add(new ZFilterAction.ZFileIntoAction(filteredPath));
/* 138 */     rules.add(new ZFilterRule("testFiltering", true, false, conditions, actions));
/* 139 */     localMbox.saveIncomingFilterRules(new ZFilterRules(rules));
/*     */     
/*     */ 
/* 142 */     ZPop3DataSource ds = getZDataSource();
/* 143 */     ds.setUsername(TEMP_USER_NAME);
/* 144 */     ds.setFolderId(dsFolder.getId());
/* 145 */     ds.setEnabled(true);
/* 146 */     localMbox.modifyDataSource(ds);
/*     */     
/*     */ 
/* 149 */     TestUtil.importDataSource(ds, localMbox, remoteMbox);
/* 150 */     List<ZMessage> messages = TestUtil.search(localMbox, "in:" + folderPath);
/* 151 */     assertEquals("Found unexpected messages in " + folderPath, 0, messages.size());
/* 152 */     messages = TestUtil.search(localMbox, "in:" + filteredPath);
/* 153 */     assertEquals("Message not found in " + filteredPath, 1, messages.size());
/*     */   }
/*     */   
/*     */   private ZPop3DataSource getZDataSource() throws Exception {
/* 157 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 158 */     List<ZDataSource> dataSources = mbox.getAllDataSources();
/* 159 */     for (ZDataSource ds : dataSources) {
/* 160 */       if (ds.getName().equals(DATA_SOURCE_NAME)) {
/* 161 */         return (ZPop3DataSource)ds;
/*     */       }
/*     */     }
/* 164 */     fail("Could not find data source " + DATA_SOURCE_NAME);
/* 165 */     return null;
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 170 */     cleanUp();
/* 171 */     TestUtil.getZMailbox("user1").saveIncomingFilterRules(this.mOriginalRules);
/*     */   }
/*     */   
/*     */   private void createDataSource() throws Exception {
/* 175 */     Provisioning prov = Provisioning.getInstance();
/* 176 */     Account account = TestUtil.getAccount("user1");
/* 177 */     int port = Integer.parseInt(TestUtil.getServerAttr("zimbraPop3BindPort"));
/* 178 */     Map<String, Object> attrs = new HashMap();
/* 179 */     attrs.put("zimbraDataSourceEnabled", "FALSE");
/* 180 */     attrs.put("zimbraDataSourceHost", "localhost");
/* 181 */     attrs.put("zimbraDataSourcePort", Integer.toString(port));
/* 182 */     attrs.put("zimbraDataSourceUsername", "user1");
/* 183 */     attrs.put("zimbraDataSourcePassword", "test123");
/* 184 */     attrs.put("zimbraDataSourceFolderId", Integer.toString(2));
/* 185 */     attrs.put("zimbraDataSourceConnectionType", "cleartext");
/* 186 */     attrs.put("zimbraDataSourceLeaveOnServer", "FALSE");
/* 187 */     prov.createDataSource(account, DataSourceType.pop3, DATA_SOURCE_NAME, attrs);
/*     */   }
/*     */   
/*     */   private DataSource getDataSource() throws Exception {
/* 191 */     Provisioning prov = Provisioning.getInstance();
/* 192 */     Account account = TestUtil.getAccount("user1");
/* 193 */     return prov.get(account, Key.DataSourceBy.name, DATA_SOURCE_NAME);
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 198 */     Provisioning prov = Provisioning.getInstance();
/* 199 */     DataSource ds = getDataSource();
/* 200 */     if (ds != null) {
/* 201 */       Account account = TestUtil.getAccount("user1");
/* 202 */       if (this.mIsServerSideTest) {
/* 203 */         Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
/* 204 */         DbPop3Message.deleteUids(mbox, ds.getId());
/*     */       }
/* 206 */       prov.deleteDataSource(account, ds.getId());
/*     */     }
/* 208 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/* 209 */     TestUtil.deleteAccount(TEMP_USER_NAME);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 214 */     TestUtil.cliSetup();
/* 215 */     TestUtil.runTest(TestPop3Import.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestPop3Import.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */